package org.hadoop.input.line;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 使用自定义LineNumberTextInputFormat读取文本文件，返回行号+行数据
 */
public class LineNumberMR extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        FileSystem fs = FileSystem.get(conf);
        Path dest = new Path("D:/a/out001");
        if(fs.exists(dest)){
            fs.delete(dest,true);
        }
        Job job = Job.getInstance(conf,"LineNumberMR");
        job.setJarByClass(this.getClass());
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(Text.class);
        //设置输入流
        job.setInputFormatClass(LineNumberInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.setInputPaths(job,new Path("D:/a/a.txt"));
        FileOutputFormat.setOutputPath(job,dest);
        job.setNumReduceTasks(2);
        return job.waitForCompletion(true)?0:1;
    }
    public static void main(String[] args) throws Exception {
        int run = ToolRunner.run(new LineNumberMR(), args);
        System.exit(run);
    }
}